
function Can_Game()
{
	Can_Array.apply(this, arguments);
}
extend_usemid(Can_Game, Can_Array);

function Game_Info(owner)
{
	Fact_Info.apply(this, arguments);
	// 所有者
	this.owner = owner;
	// ui
	this.ui = new Can_UI();
	this.ui_allotHero = new Can_UI();
	// data
	this.player_position = new Array();
}
extend_usemid(Game_Info, Fact_Info);

/**
 * 初始化一切绘图需要的信息
 */
Game_Info.prototype.init = function()
{
	this.ui.datas["cardstack"] = new UI_Normal(1, "cardstack",
			"media/images/ui/game/cardstack.png", new Position_Normal(20, 440,
					88, 127));

	this.ui.datas["ok"] = new UI_Normal(1, "ok", "media/images/ui/game/ok.png",
			new Position_Normal(700, 450, 60, 28));
	this.ui.datas["ok_disabled"] = new UI_Normal(1, "ok_disabled",
			"media/images/ui/game/ok-disabled.png", new Position_Normal(700,
					450, 60, 28));
	this.ui.datas["cancel"] = new UI_Normal(1, "cancel",
			"media/images/ui/game/cancel.png", new Position_Normal(700, 480,
					60, 28));
	this.ui.datas["cancel_disabled"] = new UI_Normal(1, "cancel_disabled",
			"media/images/ui/game/cancel-disabled.png", new Position_Normal(
					700, 480, 60, 28));

	this.ui.datas["allothero"] = new UI_Normal(1, "allothero",
			"media/images/ui/game/allothero_btn.png", new Position_Normal(725,
					10, 190, 50));
	// 弹出框
	this.ui_allotHero.datas[0] = new UI_Normal(1, "ui_chooserole_10",
			"media/images/ui/game/select_10.png", new Position_Normal(190, 115,
					510, 338));
	// 不可见
	this.ui_allotHero.update("view_state", 0, 1);
	
	

	// ////

	this.player_position[0] = new Position_Normal(790, 400, 125, 181);
	this.player_position[1] = new Position_Normal(790, 170, 125, 181);
	this.player_position[2] = new Position_Normal(20, 10, 125, 181);
	this.player_position[3] = new Position_Normal(210, 10, 125, 181);

}

// ////////////////////////////////////////
// ////////////////////////////////////////
// ////////////////////////////////////////
// ////////////////////////////////////////
// ////////////////////////////////////////

function Game_Base(a_id, a_factid)
{
	// 都继承fact_game对象
	Fact_Scene.apply(this, arguments);
	// 玩家
	this.players = new Can_Player();
	// 出手时间
	this.game_steptime = 15;
	// 游戏类型
	this.game_type = null;
	// 所有卡牌种类
	this.card_kind = new Array();
	// 所在房间的信息
	this.room = null;
	// 静态 界面的配置
	this.view = new ViewConf_Game();
	// 游戏绘图信息
	this.fact_info = new Game_Info(this);
}
extend_usemid(Game_Base, Fact_Scene);

// -----------------draw--------

Game_Base.prototype.run = function()
{
	var _time = 1;
	var _this = this;

	this.timer = setInterval(function()
	{
		if(_this.onoff)
		{
			_this.view.draw();
		}
	}, _time);
}


// 游戏行为

/**
 * 开始游戏
 */
Game_Base.prototype.init = function(aContext)
{
	Fact_Scene.prototype.init.apply(this, arguments);
	this.context = aContext;
	// 卧底this
	this.intern(this.context, "game");
	this.run();
	this.view.facts.datas["data_players"].relevant(this.players, "[\"user\"]");
	this.view.facts.datas["data_users_nickname"].relevant(this.players, "[\"user\"]");
}

/**
 * 分配武将
 */
Game_Base.prototype.allotHero = function(a_pro)
{
	// 数据OK
	this.context.player.parseFromPro(a_pro);
	// 配置选武将时武将卡的位置
	for ( var k in this.context.player.heros.datas)
	{
		this.context.player.heros.datas[k].position = new Position_Normal(190,
				115, 80, 117, (43 + (80 + 6) * k), 60);
	}
	
	this.views.add(this.context.player.heros.datas);
	this.fact_info.ui_allotHero.update("view_state", 0, 0);
	
	// 添加可点
	this.updateViewResource(this.context.player.heros.datas, 0);
	
}

/**
 * 选定武将
 */
Game_Base.prototype.chooseHero = function(a_pro)
{
	this.players.parseFromPro(a_pro);
	// 取消选武将卡的点击
	this.context.player.heros.update("op_state", 0, 1);
	// 取消选武将的界面框
	this.fact_info.ui_allotHero.update("view_state", 0, 1);
	// 取消显示选武将的卡片
	this.context.player.heros.setUnsee();
	// 取消显示头像
	for(var i in this["players"]["datas"])
	{
		this["players"]["datas"][i].user.update("view_state", 0, 1);
	}
}

Game_Base.prototype.allotCardHand = function(a_pro)
{
	// 数据OK
	this.context.player.parseFromPro(a_pro);
	this.context.player.card_hand.locate_CardHand();
	this.context.player.drawContent("card_hand:datas-Self");
	// 加入点击检测
	this.updateViewResource(this.context.player.card_hand.datas, 0);
}

/**
 * 点击事件捕获 self是canvas
 */
Game_Base.prototype.onClick = function(evt, attach)
{
	var _target_unit = this.findSelect(evt);
	var _target = _target_unit.link;
	alert("选中：" + _target["fact_id"] + " " + _target["object_type"]);
	
	var _proBean = null;

	switch (_target["object_type"])
	{
	// ui
	case 'UI_Normal':
		switch (_target["fact_id"])
		{
		case 'ok':
			break;
		case 'ok_disabled':
			break;
		case 'cancel':
			break;
		case 'cancel_disabled':
			break;
		case 'allothero':

			alert("分配可选武将");

			_proBean = new Pro_AllotHero();
			this.context.send(_proBean);

			break;
		default:
			break;
		}
		break;

	case 'Hero_Normal':
		alert("选择:" + _target["hero_name"]);
		_proBean = new Pro_ChooseHero(_target["fact_id"]);
		this.context.send(_proBean);
		break;

	case 'Player_Normal':
		alert("选择:" + _target["user"]["user_nickname"]);
		break;

	case 'Card_Normal':
		alert("选择:" + _target["card_name"]);
		break;

	default:
		break;
	}
}

/**
 * onRecv
 */
Game_Base.prototype.onRecv = function(msg)
{
	 alert("on Room Recv:" + msg.data);
	var parsed = JSON.parse(msg.data);
	switch (parsed.pro_id)
	{
	// 分配武将
	case '1060':
		this.allotHero(parsed["player"]);
		break;
	// 选定武将
	case '1062':
		this.chooseHero(parsed["players"]);
		break;
	case '1064':
		this.allotCardHand(parsed);
		break;
	case 'error':
		alert("error");
		break;
	default:
		throw new Error('Unknown message type ' + parsed.pro_id);
		break;
	}
}

function Game_Normal(a_id, a_factid, a_gametype, a_room)
{
	Game_Base.apply(this, [ a_id, a_factid ]);

	this.room = a_room;
	this.game_type = a_gametype;
}
extend_usemid(Game_Normal, Game_Base);
